
package ManagerInfoFile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ListChunk {
    private String fileName;
    private Chunk[] danhSanhChunk;
    private int soLuongFileChunkHoanhThanh;
    public ListChunk()
    {
        this.fileName = null;
        this.danhSanhChunk = null;
        this.soLuongFileChunkHoanhThanh = 0;
    }
    public void SetFileName(String filename)
    {
        this.fileName = filename;
    }
    public String GetFileName()
    {
        return this.fileName;
    }
    public int GetLength()
    {
        return this.danhSanhChunk.length;
    }
    public Chunk GetChunkinfo(int index)
    {
       if(index >= 0 && index < this.danhSanhChunk.length)
           return danhSanhChunk[index];
       return null;
    }
    public void ReadFileTorrent(String urlFileName)
    {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(urlFileName);
            BufferedReader br = new BufferedReader(fileReader);
            List<String> listFileName;
            String line;
            line = br.readLine();
            
            // set file Name
            if(line!=null)
            {
                SetFileName(line);
            }
            
            listFileName = new ArrayList<String>();
            while((line = br.readLine()) !=  null)
            {
                listFileName.add(line+".chunk");
            }
            fileReader.close();
            //khoi tao
            
            KhoiTao(listFileName.size());
          
            for(int i = 0; i < listFileName.size();i++)
            {        
                danhSanhChunk[i].SetTenFile(listFileName.get(i));
                danhSanhChunk[i].SetTinhTrang("Chờ");
            }
            System.out.println("Danh sach "+danhSanhChunk[0].GetTenFile());

            
        } catch (FileNotFoundException ex) {
            System.out.println("Lỗi, Không tìm Thấy File Node "+ex.getMessage());
        } catch(IOException io){
            System.out.println("Lỗi, Đọc Dữ Liệu File Node "+io.getMessage());
        }
    }
    public void KhoiTao(int kichthuoc)
    {
        this.danhSanhChunk = new Chunk[kichthuoc];
        for(int i=0; i<kichthuoc;i++)
        {
            this.danhSanhChunk[i] = new Chunk();
        }
        this.soLuongFileChunkHoanhThanh = 0;
    }
    public void SetSoLuongFileChunkDownloadThanhCong( )
    {
       // if(chunk.GetTinhTrang())
        {
            this.soLuongFileChunkHoanhThanh = this.soLuongFileChunkHoanhThanh + 1;
        }
    }
    public Boolean KiemTraFileHoanThanh()
    {
        if(this.soLuongFileChunkHoanhThanh == this.danhSanhChunk.length)
            return true;
        return false;
    }

}
